﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibraryONTrack.dto;
using LibraryONTrack.dao;

namespace LibraryONTrack.BAL
{
	[Serializable]
public class BookManagerException : Exception
{
  public BookManagerException() { }
  public BookManagerException( string message ) : base( message ) { }
  public BookManagerException( string message, Exception inner ) : base( message, inner ) { }
  protected BookManagerException( 
	System.Runtime.Serialization.SerializationInfo info, 
	System.Runtime.Serialization.StreamingContext context ) : base( info, context ) { }
}

	[Serializable]
public class BookManagerNotFoundException : BookManagerException
{
  public BookManagerNotFoundException() { }
  public BookManagerNotFoundException( string message ) : base( message ) { }
  public BookManagerNotFoundException( string message, Exception inner ) : base( message, inner ) { }
}

	[Serializable]
public class BookManagerAlreadyExistsException : BookManagerException
{
  public BookManagerAlreadyExistsException() { }
  public BookManagerAlreadyExistsException( string message ) : base( message ) { }
  public BookManagerAlreadyExistsException( string message, Exception inner ) : base( message, inner ) { }
}

	public class BookManager
	{

		public BookManager()
		{

		}

		public List<Book> GetAll()
		{
			return new BookDAO().GetAll();
		}

		public Book GetBook(string title)
		{
			Book book = new BookDAO().GetByName(title);
			if (book == null)
			{
				throw new BookManagerNotFoundException(title);
			}
			return book;
		}

		public Book GetById(long id)
		{
			return new BookDAO().GetById(id);
		}

		public Book AddBook(string title)
		{
			try 
			{	        
				GetBook(title);
				throw new BookManagerAlreadyExistsException(title);
			}
			catch (BookManagerNotFoundException)
			{
				return (Book) new BookDAO().Insert(new Book(title));
			}
		}
	}
}
